const express = require('express');
const r = express.Router();
const pool = require('../pool.js');
// 测试
r.get('/cs',(req,res)=>{
	res.send('laptop服务器成功开启')
});

// 添加商品分组
r.post('/v1/lapFamily',(req,res)=>{
	var obj = req.body;
	
	sql = 'insert into xz_laptop_family set ?';
	pool.query(sql, [obj], (err,result)=>{
		if(err) throw err;
		if(result.affectedRows===0){
			res.send("0");
		}else{
			res.send("1");
		}
	});
});

// 查询商品分组
r.get("/v1/lapFamilyList",(req,res)=>{
	// var $lid = req.params.lid;

	sql = "SELECT * FROM xz_laptop_family"
	pool.query(sql,(err,result)=>{
			if(err) throw err;
			if(result.length>0){
				res.send(result);
			}else{
				res.send("0");
			}
		});
});
// 添加商品
r.post('/v1/lapAdd',(req,res)=>{
	var obj = req.body;
	
	sql = 'insert into xz_laptop set ?';
	pool.query(sql, [obj], (err,result)=>{
		if(err) throw err;
		if(result.affectedRows===0){
			res.send("0");
		}else{
			res.send("1");
		}
	});
});

// 修改商品
r.put("/v1/lapUpdate",(req,res)=>{
     var obj = req.body;
	console.log(req.body);
	sql = 'UPDATE xz_laptop SET ? WHERE lid = ?';
	pool.query(sql,[obj,obj.lid],(err,result)=>{
		if(err) throw err;
		if(result.affectedRows===0){
			res.send("0");
		}else{
			res.send("1");
		}
	});
});

// 根据lid删除商品
r.delete("/v1/lapDel/:lid",(req,res)=>{
	var $lid = req.params.lid;
	
	sql = "delete from xz_laptop where lid=?"
	pool.query(sql,[$lid],(err,result)=>{
		if(err) throw err;
		if(result.affectedRows===0){
			res.send("0");
		}else{
			res.send("1");
		}
	});
});

// 根据lid查询商品详情
r.get("/v1/lapDetail/:lid",(req,res)=>{
	var $lid = req.params.lid;

	sql = "select lid,family_id,title,subtitle,price,promise,spec,lname,os,memory,resolution,video_card,cpu,video_memory,category,disk,details,shelf_time,sold_count,is_onsale from xz_laptop where lid=?"
	pool.query(sql,[$lid],(err,result)=>{
		if(err) throw err;
		if(result.length>0){
			res.send(result);
		}else{
			res.send("0");
		}
	});
});

// 分页商品列表
r.get("/v1/lapList/:pno&:count",(req,res)=>{
	var $pno = req.params.pno;
	var $count = req.params.count;
    	$pno = parseInt(($pno));
     	$count = parseInt(($count));
	
	sql = "select lid,family_id,title,subtitle,price,promise,spec,lname,os,memory,resolution,video_card,cpu,video_memory,category,disk,details,shelf_time,sold_count,is_onsale from xz_laptop limit ?,?"
	pool.query(sql,[$pno,$count],(err,result)=>{
		if(err) throw err;
		if(result.length>0){
			res.send(result);
		}else{
			res.send("0");
		}
	});
});



module.exports = r